using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 对Transform 的拓展方法
/// </summary>
public static class TransformTools
{



    public static void RemoveAllChildren(this Transform parentContanier)
    {

        // List<Transform> childrens = new List<Transform>();
        // foreach (Transform item in parentContanier.transform)
        // {
        //     childrens.Add(item);
        // }
        // for (int i = 0; i < childrens.Count; i++)
        // {
        //     GameObject.Destroy(childrens[i].gameObject);
        // }

        for (int i = 0; i < parentContanier.transform.childCount; i++)
        {
            GameObject.Destroy(parentContanier.transform.GetChild(i).gameObject);
        }
        parentContanier.transform.DetachChildren();

        return;
    }


    public static void SortChildrenName(this Transform tansformParent)
    {
        List<Transform> list = new List<Transform>();
        foreach (Transform item in tansformParent)
        {
            list.Add(item);
        }
        list.Sort((Transform a,Transform b)=>
        {
            Debug.Log(a.gameObject.name);
            Debug.Log(b.gameObject.name);
            if(a.gameObject.name.Length > b.gameObject.name.Length)
            {
                return 1;
            }else
            {
                return -1;
            }
            return 0;
        });
        for (int i = 0; i < list.Count; i++)
        {
            list[i].SetSiblingIndex(i);
        }

    }



    public static GameObject FindSearchChildren(this Transform tansformParent,string Name)
    {
        for(int i = 0;i < tansformParent.childCount;i++)
        {
            if(tansformParent.GetChild(i).gameObject.name == Name)
            {
                return tansformParent.GetChild(i).gameObject;
            }else
            {
                if(tansformParent.GetChild(i).childCount > 0)
                {
                    return tansformParent.GetChild(i).FindSearchChildren(Name);
                }
            }
        }

        return null;

    }



}
